#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :logger.py
# Time  :2024/11/26 16:28
# Email :fortunatedong@163.com
# Author:by Adonli
import sys
from datetime import time

from loguru import logger

folder_ = "Log/"
rotation_ = time(0, 0)
retention_ = "30 days"
encoding_ = "utf-8"
backtrace_ = True
diagnose_ = False
enqueue_=True
format_ = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> ' \
            '| <magenta>{process}</magenta>:<yellow>{thread}</yellow> ' \
            '| <cyan>{name}</cyan>:<cyan>{function}</cyan>:<yellow>{line}</yellow> - <level>{message}</level>'


class Loggings():
    __instance = None

    # 日志配置
    FOLDER = "Log/"
    ROTATION_TIME = time(0, 0)  # 设置为午夜 00:00
    RETENTION = "30 days"
    ENCODING = "utf-8"
    BACKTRACE = True
    DIAGNOSE = False
    ENQUEUE = True
    FORMAT = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> ' \
             '| <magenta>{process}</magenta>:<yellow>{thread}</yellow> ' \
             '| <cyan>{name}</cyan>:<cyan>{function}</cyan>:<yellow>{line}</yellow> - <level>{message}</level>'

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(Loggings, cls).__new__(cls, *args, **kwargs)
            # 初始化日志配置
            cls._initialize_logger()
        return cls.__instance

    @classmethod
    def _initialize_logger(cls):
        # 移除所有已存在的处理器
        logger.remove()

        # 添加文件处理器
        logger.add(
            cls.FOLDER + "JD_order_synchronization_kuaipu.log",
            rotation=cls.ROTATION_TIME,  # 使用 time 对象设置rotation时间
            retention=cls.RETENTION,
            encoding=cls.ENCODING,
            backtrace=cls.BACKTRACE,
            diagnose=cls.DIAGNOSE,
            enqueue=cls.ENQUEUE,
            format=cls.FORMAT,
            colorize=False
        )

        # 添加控制台处理器
        logger.add(
            sys.stderr,
            format=cls.FORMAT,
            colorize=True,
            backtrace=cls.BACKTRACE,
            diagnose=cls.DIAGNOSE,
            enqueue=cls.ENQUEUE
        )

    def info(self, msg):
        return logger.info(msg)

    def debug(self, msg):
        return logger.debug(msg)

    def warning(self, msg):
        return logger.warning(msg)

    def error(self, msg):
        return logger.error(msg)
